/**
 * Created by miracle on 2017/7/11.
 */
$(function () {

    var pageHeight,
        start = 0,
        maxLen = 8,
        dataLength;
    // 初始化
    getDate();
    function getDate() {
        //国内折扣商品列表
        $.get('http://127.0.0.1:3000/api/getinlanddiscount', function (data) {
            // console.log(data);
            dataLength = data.result.length;
            var str = ''
            for (var i = len; i < maxLen; i++) {
                str += '<li>' +
                    '<div>' + data.result[i].productImg + '</div>' +
                    '<h4>' + data.result[i].productName + '</h4>' +
                    '<p>' + data.result[i].productPrice + '</p>' +
                    '<span>' + data.result[i].productFrom + '</span>' +
                    '<span>' + data.result[i].productTime + '</span>' +
                    '</li>';

            }
            $('.discount').append(str);
            pageHeight = $(document).height();
            console.log(pageHeight);//3041
        })
    }

//图标懒加载
    var wHeight = $(window).height();
    console.log(wHeight);      //738
    /*  var dHeight = $(document).height();
     console.log(dHeight);*/    //738

    $(window).scroll(function () {
        if ($(window).scrollTop() == (pageHeight - wHeight)) {

            if (maxLen < dataLength) {
                getDate();
                len = maxLen;
                maxLen += 4;
            } else {
                console.log('所有数据已经加载完毕');
            }
        }
    });


    //返回顶部
    $('.back').on('click', function () {
        $('html,body').animate({scrollTop: 0}, 1500);
    })
})